package com.XSQS.storage.index;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class IndexEntry {
	public static final short LENGTH = 32;

	private int docID;
	private int sIndex;
	private int type;
	private int parentID;
	private int nameID;
	private int offset;
	private int len;
	private int next;

	public void build(byte[] data) {
		if (data.length == IndexEntry.LENGTH) {
			DataInputStream dis = new DataInputStream(new ByteArrayInputStream(
					data));
			try {
				setDocID(dis.readInt());
				setSIndex(dis.readInt());
				setType(dis.readInt());
				setParentID(dis.readInt());
				setNameID(dis.readInt());
				setOffset(dis.readInt());
				setLen(dis.readInt());
				setNext(dis.readInt());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public byte[] toByteArray() {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(
				IndexEntry.LENGTH);
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			dos.writeInt(getDocID());
			dos.writeInt(getSIndex());
			dos.writeInt(getType());
			dos.writeInt(getParentID());
			dos.writeInt(getNameID());
			dos.writeInt(getOffset());
			dos.writeInt(getLen());
			dos.writeInt(getNext());
		} catch (IOException ex) {
			ex.printStackTrace();
			return null;
		}
		return baos.toByteArray();
	}

	public void setDocID(int docID) {
		this.docID = docID;
	}

	public int getDocID() {
		return docID;
	}

	public void setSIndex(int sIndex) {
		this.sIndex = sIndex;
	}

	public int getSIndex() {
		return sIndex;
	}

	public void setNameID(int nameID) {
		this.nameID = nameID;
	}

	public int getNameID() {
		return nameID;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getOffset() {
		return offset;
	}

	public void setLen(int len) {
		this.len = len;
	}

	public int getLen() {
		return len;
	}

	public void setParentID(int parentID) {
		this.parentID = parentID;
	}

	public int getParentID() {
		return parentID;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getType() {
		return type;
	}

	public void setNext(int next) {
		this.next = next;
	}

	public int getNext() {
		return next;
	}

	public boolean equals(Object anObject) {
		if (this == anObject) {
			return true;
		}
		if (anObject instanceof IndexEntry) {
			IndexEntry anotherEntry = (IndexEntry) anObject;
			return (this.docID == anotherEntry.docID && this.sIndex == anotherEntry.sIndex);
		}
		return false;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(docID + " | ");
		sb.append(sIndex + " | ");
		sb.append(type + " | ");
		sb.append(parentID + " | ");
		sb.append(nameID + " | ");
		sb.append(offset + " | ");
		sb.append(len + " | ");
		sb.append(next);
		return sb.toString();
	}
}
